package com.example.echoewish.ui.my_activity.friend_request_activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.Toast;

import com.example.echoewish.R;
import com.example.echoewish.econstant.Constant;
import com.example.echoewish.pojo.MyPreConcernRequest;
import com.example.echoewish.ui.EchoWishCompatActivity;

import java.util.LinkedList;
import java.util.List;

public class FriendRequestActivity extends EchoWishCompatActivity {

    private ListView friendRequestListView;
    private FriendRequestAdapter friendRequestAdapter;
    private FriendRequestViewModel friendRequestViewModel;
    private int nowPosition;


    private Handler friendRequestActivityHandler=new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what)
            {
                case Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE:
                    Toast.makeText(FriendRequestActivity.this,"获取好友请求出错",Toast.LENGTH_SHORT).show();
                    break;
                case Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE:
                    friendRequestAdapter.clear();
                    friendRequestAdapter.addAll(friendRequestViewModel.myPreConcernRequestMutableLiveData.getValue());
                    friendRequestAdapter.notifyDataSetChanged();
                    break;
                case FriendRequestViewModel.ACCEPT_SUCCESS:
                    friendRequestAdapter.remove(friendRequestAdapter.getItem(nowPosition));
                    friendRequestAdapter.notifyDataSetChanged();
                    break;
                case FriendRequestViewModel.REFUSE_SUCCESS:
                    friendRequestAdapter.remove(friendRequestAdapter.getItem(nowPosition));
                    friendRequestAdapter.notifyDataSetChanged();
                        break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        friendRequestAdapter.setButtonClickListener(new FriendRequestAdapter.ButtonClickListener() {
            @Override
            public void acceptButtonClickListener(int nowPosition) {
                FriendRequestActivity.this.nowPosition=nowPosition;
                friendRequestViewModel.acceptMutableLiveData.postValue(friendRequestAdapter.getItem(nowPosition).hostId);
            }

            @Override
            public void refuseButtonClickListener(int nowPosition) {
                FriendRequestActivity.this.nowPosition=nowPosition;
                friendRequestViewModel.refuseMutableLiveData.postValue(friendRequestAdapter.getItem(nowPosition).hostId);
            }
        });
        friendRequestViewModel.getFriendRequest();
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_friend_request);
    }

    @Override
    public void initView() {
        friendRequestListView=findViewById(R.id.friend_request_list_view);
        friendRequestAdapter=new FriendRequestAdapter(this,R.layout.friend_request_item,new LinkedList<>());
        friendRequestListView.setAdapter(friendRequestAdapter);
    }

    @Override
    public void initViewModel() {
        friendRequestViewModel=new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(FriendRequestViewModel.class);
    }

    @Override
    public void observeLiveData() {

        friendRequestViewModel.myPreConcernRequestMutableLiveData.observe(this, new Observer<List<MyPreConcernRequest>>() {
            @Override
            public void onChanged(List<MyPreConcernRequest> myPreConcernRequests) {
                Message msg=new Message();
                if(myPreConcernRequests==null)
                {
                    friendRequestViewModel.myPreConcernRequestMutableLiveData.postValue(new LinkedList<>());
                    msg.what= Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE;
                    friendRequestActivityHandler.sendMessage(msg);
                }
                if(myPreConcernRequests.size()==0)
                    return;
                else
                {
                    msg.what= Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE;
                    friendRequestActivityHandler.sendMessage(msg);
                }
            }
        });

        friendRequestViewModel.acceptMutableLiveData.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                friendRequestViewModel.acceptFriendRequest();
            }
        });

        friendRequestViewModel.refuseMutableLiveData.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                friendRequestViewModel.refuseFriendRequest();
            }
        });

        friendRequestViewModel.responseMutableLiveData.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                Message msg=new Message();
                msg.what=integer;
                friendRequestActivityHandler.sendMessage(msg);
            }
        });

    }
}